#!/usr/bin/python
#  This is part of Scopia Visual Framework
#
#  This module has two implementations for rendering text. One
#  which renders text to a buffer using truetype for each text and
#  another which prerenders every character spearately and then draws
#  quads with the needed characters for each one in a text."""
#
#  Copyright (C) 2008  Scopia Visual Interfaces Systems 
#                      http://www.scopia.es
#
#  author: Alejandro Conty
#  contributors:
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

import render
from numpy import *

class textureText_t(object):
    """Directly rendered text object. Builds a texture, renders the
       text inside and the displays it with a quad. Use with caution."""

    def __init__( self, context, text, width, height ):
        """Init function
                
                context         glContext to use
                text            Text to render
                width, height   Available space, doesn't matter the units we
                                only care about the ratio"""
        img = render.render( text, width, height )
        self.texid = context.loadTextureImg( img )

    def draw(self, context, points):
        """Draw text in a context using points as the quad. It has to be a numpy
           float array of the shape (n, 3)."""
        uv = array( ( (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0) ), 'f' )
        context.startTextureId( self.texid )
        context.drawQuadArray( points, uv )
        context.endTextureId( self.texid )

    def reset(self, context):
        """Destroy the allocated texture. Must be down explicitly"""
        context.eraseTextureId( self.texid )
        del self.texid
    
    def __del__(self):
        try:
            id = self.texid
            print 'WARNING undestroyed text texture'
        except: pass
